package com.ruoyi.web.controller.basedata;

import com.ruoyi.basedata.domain.DwAreas;
import com.ruoyi.basedata.domain.LouDong;
import com.ruoyi.basedata.service.IDwAreasService;
import com.ruoyi.basedata.service.ILouDongService;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.CollectionUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import io.swagger.v3.oas.annotations.Parameter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

/**
 * 楼栋Controller
 * 
 * @author ruoyi
 * @date 2023-09-07
 */
@RestController
@RequestMapping("/basedata/loudong")
public class LouDongController extends BaseController
{
    @Autowired
    private ILouDongService louDongService;

    @Autowired
    private IDwAreasService areasService;

    /**
     * 查询楼栋列表
     */
    @PreAuthorize("@ss.hasPermi('basedata:loudong:list')")
    @GetMapping("/list")
    public TableDataInfo list(LouDong louDong)
    {
        startPage();
        List<LouDong> list = louDongService.selectLouDongList(louDong);
        return getDataTable(list);
    }

    //根据区域id获取楼栋信息
    @PreAuthorize("@ss.hasPermi('basedata:loudong:query')")
    @GetMapping("/getByAreaId")
    @Parameter(name = "areaId", description = "编号", required = true)
    public AjaxResult list(@RequestParam("areaId") Long areaId)
    {
        List<LouDong> list = louDongService.selectListByAreaId(areaId);
        List<String> areaCodeList = CollectionUtils.convertList(list, LouDong::getAreaCode);
        Map<String, DwAreas> areaMap = areasService.getAreaMap(areaCodeList);
        list.forEach(louDong1 -> {
            louDong1.setAreaName(areaMap.get(louDong1.getAreaCode()).getAreaName());
        });
        return AjaxResult.success(list);
    }

    //根据areacode获取楼栋信息
    @PreAuthorize("@ss.hasPermi('basedata:loudong:query')")
    @GetMapping("/getByAreaCode")
    @Parameter(name = "areaCode", description = "编号", required = true)
    public AjaxResult listByAreaCode(@RequestParam("areaCode") String areaCode)
    {
        List<LouDong> list = louDongService.selectListByAreaCode(areaCode);
        List<String> areaCodeList = CollectionUtils.convertList(list, LouDong::getAreaCode);
        Map<String, DwAreas> areaMap = areasService.getAreaMap(areaCodeList);
        list.forEach(louDong1 -> {
            louDong1.setAreaName(areaMap.get(louDong1.getAreaCode()).getAreaName());
        });
        return AjaxResult.success(list);
    }

    /**
     * 导出楼栋列表
     */
    @PreAuthorize("@ss.hasPermi('basedata:loudong:export')")
    @Log(title = "楼栋", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, LouDong louDong)
    {
        List<LouDong> list = louDongService.selectLouDongList(louDong);
        ExcelUtil<LouDong> util = new ExcelUtil<LouDong>(LouDong.class);
        util.exportExcel(response, list, "楼栋数据");
    }

    /**
     * 获取楼栋详细信息
     */
    @PreAuthorize("@ss.hasPermi('basedata:loudong:query')")
    @GetMapping(value = "/{ID}")
    public AjaxResult getInfo(@PathVariable("ID") Long ID)
    {
        LouDong louDong = louDongService.selectLouDongByID(ID);
        DwAreas dwAreas = areasService.selectDwAreasByAreaCode(louDong.getAreaCode());
        louDong.setAreaName(dwAreas.getAreaName());
        return success(louDong);
    }

    /**
     * 新增楼栋
     */
    @PreAuthorize("@ss.hasPermi('basedata:loudong:add')")
    @Log(title = "楼栋", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody LouDong louDong)
    {
        int flag = louDongService.insertLouDong(louDong);
        if (flag == 0){
            return error("该区域下已有相同名称的楼栋");
        }
        return toAjax(flag);
    }

    /**
     * 修改楼栋
     */
    @PreAuthorize("@ss.hasPermi('basedata:loudong:edit')")
    @Log(title = "楼栋", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody LouDong louDong)
    {
        return toAjax(louDongService.updateLouDong(louDong));
    }

    /**
     * 删除楼栋
     */
    @PreAuthorize("@ss.hasPermi('basedata:loudong:remove')")
    @Log(title = "楼栋", businessType = BusinessType.DELETE)
	@DeleteMapping("/{IDs}")
    public AjaxResult remove(@PathVariable Long[] IDs)
    {
        int flag = louDongService.deleteLouDongByIDs(IDs);
        if (flag == 0){
            return error("该楼栋下存在房间，请先删除房间");
        }
        return toAjax(flag);
    }

    @GetMapping("/optionselect")
    public AjaxResult optionselect(LouDong louDong)
    {
        List<LouDong> list = louDongService.selectLouDongList(louDong);
        return success(list);
    }

    @Log(title = "导入楼栋档案", businessType = BusinessType.IMPORT)
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception
    {
        ExcelUtil<LouDong> util = new ExcelUtil<LouDong>(LouDong.class);
        List<LouDong> louDongList = util.importExcel(file.getInputStream());
        String operName = getUsername();
        String message = louDongService.importData(louDongList, updateSupport, operName);
        return success(message);
    }
}
